const index = require('../config/index.js')
export function loginByUserName (data, id) {
  wx.request({
    url: index.api +'common/oauth/token',
    data,
    method: 'post',
    success(res) {
      console.log(res)
      if(res.data.success) {
        wx.setStorageSync('token', res.data.data.token)
        wx.showToast({
          title: '登陆成功',
          icon: 'success',
          duration: 3000,
          success() {
            if(id) {
              wx.redirectTo({
                url: '../productdetail/productdetail?id=' + id,
              })
            }else {
              wx.reLaunch({
                url: '../personal/personal'
              })
            }
          }
        })
      }else {
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
      }
    }
  })
}
export function loginOut() {
  wx.removeStorageSync('token')
}
export default {
  loginByUserName,
  loginOut
}